// pages/loupan/detail.js
var app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
  data: {
    id:'',
    detail: '',
    moretext: "点击查看更多",
    showmore: false,
    liked: false,
    imglist: '',
    wuyetype: '', // 物业类型
    wenda: [],
    modalName:'',
    picker: [
      '男', '女'
    ],
    value: '男',
		active:0,
		houseid:'',
		name:'',
		tel:'',
  },

	/**
	 * 生命周期函数--监听页面加载 
	 */
  onLoad: function (options) {
    console.log(options);
    var that = this;
    that.setData({
      id: options.id
    })
    that.getdetail()
  },
  getdetail(){
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var id =that.data.id;
    app.core('get','get_sale_online_detail/id/'+id+'','',(res=>{
      console.log(res,'详情')
      that.setData({
        detail:res.data
      })
      wx.hideLoading()
    }))
  },


  // detail() {
  //   var id = this.data.id;
  //   var that = this;
  //   var opid = wx.getStorageSync('user').openid;
  //   app.core('get', 'get_houses_detail/id/' + id + '/open_id/' + opid + '', {}
  //     , (res) => {
  //       console.log(res)

  //       var newres = res.data;
  //       var wuyetype = res.data.propertyType;
  //       switch (wuyetype) {
  //         case "-1-3-":
  //           wuyetype = "-住宅-写字楼-"
  //           break;
  //         case "-1-2-":
  //           wuyetype = "-住宅-商铺-"
  //           break;
  //         case "-1-2-3":
  //           wuyetype = "-住宅-商铺-写字楼-"
  //           break;
  //         case "-2-3-":
  //           wuyetype = "-商铺-写字楼-"
  //           break;
  //       }


  //       var arr = [];
  //       if (res.data.buildNoPhoto) {
  //         arr.push(res.data.buildNoPhoto);
  //       }
  //       if (res.data.effectPhoto) {
  //         arr.push(res.data.effectPhoto);
  //       }
  //       if (res.data.trafficPhoto) {
  //         arr.push(res.data.trafficPhoto);
  //       }
  //       if (res.data.dimensionPhoto) {
  //         arr.push(res.data.dimensionPhoto);
  //       }
  //       if (res.data.videoPhoto) {
  //         arr.push(res.data.videoPhoto);
  //       }
  //       that.setData({
  //         wuyetype: wuyetype,
  //         detail: res.data,
  //         liked: res.hasCollect
  //       })
  //       that.setData({
  //         imglist: arr
  //       })
  //       var x = res.data.loupan_wenda;
  //       var wenda = [];
  //       for (var i = 0; i < x.length; i++) {
  //         if (x[i].p_wenda_id == 0) {
  //           wenda.push(x[i])
  //         }
  //       }
  //       that.setData({
  //         wenda: wenda
  //       })
  //       console.log(arr)
  //     }
  //   )
  // },// 获取详情

	
  btnliked() {

    var that = this;
    var liked = that.data.liked;
    if (liked == false) {
      that.liked()
    } else {
      that.dislike()
    }
  },
  liked() {
    wx.showLoading({
      title: '加载中',
    })
    var opid = wx.getStorageSync('user').openid;
    var id = this.data.id;
    var liked = this.data.liked;

    app.core('get', 'create_user_collect/open_id/' + opid + '/type/1/lid/' + id + '', '', (res => {
      this.setData({
        liked: true,
      })
      console.log(this.data.liked)
      wx.showToast({
        title: '收藏成功',
        icon: 'none',
        duration: 1000
      })
      wx.hideLoading()
    }))


  },// 收藏
  dislike() {

    var that = this;
    var id = that.data.id;
    var like = that.data.liked;
    var opid = wx.getStorageSync('user').openid;

    app.core('get', 'cancel_user_collect/open_id/' + opid + '/type/2/lid/' + id + '', "", (res => {
      console.log(res)
      wx.showToast({
        title: '取消收藏',
        icon: 'none',
        duration: 1000
      })
      that.setData({
        liked: false
      })
    }))


  },// 取消收藏

	previewImage(e) {
    var current = e.target.dataset.src;
		var arr = []
		arr.push(current);
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
			urls: arr
    })

  },//预览图片
  addmore() {
    var show = this.data.showmore;

    if (show == true) {
      console.log(show)
      this.setData({
        showmore: false,
        moretext: '点击查看更多'
      })
    }
    else if (show == false) {
      console.log(show)
      this.setData({
        showmore: true,
        moretext: '点击收起'
      })
    }

  },
  // 点击加载更多
  share() {
    wx.updateShareMenu({
      withShareTicket: true,
      success(res) {
        console.log(res)
      }
    })
  },
 
  // 分享转发
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {

  },

	/**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {

  },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function () {

  },

	/**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function () {

  },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {

  },

	/**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function () {

  },

	/**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function (res) {
    return {
      title: this.data.detail.projectName,
      path: 'pages/loupan/detail?id=' + this.data.id + ''
    }
  },
  showModal(e) {
		var houseid = e.currentTarget.dataset.id;

    this.setData({
      modalName: e.currentTarget.dataset.target,
			houseid: houseid
    })

	
	  },
	pickerchange(e) {
		console.log(e);
		var name = this.data.picker[e.detail.value]
		this.setData({
			value: name
		})
	},

	getname(e){
		this.setData({
			name: e.detail.value
		})

	},
	gettel(e){
		this.setData({
			tel: e.detail.value
		})

	},

  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
	submitmodal() {
		var project_id = this.data.houseid;
		var that = this;
		var name = that.data.name;
		var phone = that.data.tel;
		var sex = that.data.value;
		if (name == '') {
			wx.showToast({
				title: '请输入姓名',
				duration: 1000,
				icon: 'none'
			});
			return;
		} else if (phone == '') {
			wx.showToast({
				title: '请输入手机号码',
				duration: 1000,
				icon: 'none'
			})
		} else {
			app.core('post', 'order_project ', {
				name: name,
				phone: phone,
				type: 2,
				project_id: project_id,
				sex: sex
			}, (res => {
					this.setData({
						modalName: null
					})
					wx.showToast({
						title: '已申请预订',
						duration: 1000,
						icon: 'none'
					})

			}))

		}
	}
})